package com.wm.task2.view;

import com.wm.task2.dao.PostDAOFile;
import com.wm.task2.dao.UserDAOFile;
import com.wm.task2.vo.Account;
import com.wm.task2.vo.Post;

import java.util.ArrayList;
import java.util.List;


public class ViewMain extends BaseView {
    private UserDAOFile userDAOFile = new UserDAOFile();
    private PostDAOFile postDAOFile = new PostDAOFile();
    private Account account = new Account();
    private List<Post> post = new ArrayList<>();


    @Override
    protected void handleChoice(String choice) {
        switch (choice) {
            case "1"://注册
                this.register();
                break;
            case "2"://登录
                this.login();
                break;

        }
    }

    @Override
    protected String getTitle() {
        return "万码BBS";
    }

    @Override
    protected String[] getMenus() {
        String[] menus = {"注册", "登录"};
        return menus;
    }


    private void register() {
        System.out.println("===账号注册===");
        System.out.print("请输入用户名:");
        String name = sc.next();
        System.out.print("请输入密码:");
        String passWord = sc.next();
        sc.nextLine();
        account = userDAOFile.register(name, passWord);
        System.out.println("创建成功");
        System.out.println("账户名:" + account.getName());
        System.out.println("ID:" + account.getAccountNum());
        System.out.println("密码:" + account.getPassWord());
    }

    private void login() {
        System.out.println("===登录账号===");
        System.out.print("请输入账号ID:");
        String id = sc.next();
        System.out.print("请输入账号密码:");
        String passWord = sc.next();
        account = userDAOFile.login(id, passWord);

        if (account != null) {
            System.out.println("登录成功");
            int b = 1;
            while (b != 0) {
                menuII(account.getName());
                b = chooseII(account.getAccountNum());
            }


        } else {
            System.out.println("账号或密码错误");
        }
    }

    private int chooseII(String id) {
        System.out.print("请选择:");
        String choice = sc.next();
        switch (choice) {
            case "1"://发帖
                this.addPost(id);
                return 1;
            case "2"://我发过的帖
                this.myPost(id);
                return 1;
            case "3"://全站帖子列表
                allPost();

                return 1;
            case "0"://退出
                return 0;
            default:
                return 1;

        }
    }

    private void myPost(String id) {
        post = postDAOFile.myPost(id + ".txt");
        System.out.println(post.get(0).getAccount().getName() + ":");
        for (int i = 0; i < post.size(); i++) {
            System.out.println("标题:" + post.get(i).getTitle());
            System.out.println("内容:" + post.get(i).getContent());
            System.out.println(post.get(i).getTime());
        }
    }

    private void allPost() {
        List<List<Post>> posts = postDAOFile.allPost();
        while (true) {
            int count = 1;
            for (int i = 0; i < posts.size(); i++) {
                for (int j = 0; j < posts.get(i).size(); j++) {
                    System.out.println(count + ". " + posts.get(i).get(j).getTitle() +
                            "(" + posts.get(i).get(j).getAccount().getName() + posts.get(i).get(j).getTime() + ")");
                    count++;
                }
            }
            System.out.println("0. 退出");
            System.out.print("请选择:");
            int choose = sc.nextInt();
            if(choose == 0){
                return;
            }
            count = 1;
            for (int i = 0; i < posts.size(); i++) {
                for (int j = 0; j < posts.get(i).size(); j++) {
                    if(choose == count){
                        System.out.println("内容:");
                        System.out.println(posts.get(i).get(j).getContent());
                    }
                    count++;
                }
            }
        }
    }

    private void menuII(String name) {
        System.out.println("====万码BBS" + name + "===");
        System.out.println("1、发帖");
        System.out.println("2、我发过的帖");
        System.out.println("3、全站帖子列表");
        System.out.println("0、退出");
    }


    private void addPost(String id) {
        System.out.println("===发帖===");
        System.out.print("请输入标题:");
        String title = sc.next();
        System.out.println("请输入内容:");
        String content = sc.next();

        postDAOFile.addPost(id, title, content);
    }


}
